package com.ykg.service.impl;

import com.ykg.dto.CUserDTO;
import com.ykg.dto.OwnerDTO;
import com.ykg.mapper.OwnerMapper;
import com.ykg.pojo.Owner;
import com.ykg.service.IOwnerService;
import com.ykg.util.CacheUtil;
import com.ykg.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * TODO
 * 类(接口)：
 * Created on 8:57 2021/6/23.
 *
 * @Author: laobian
 * @Description:
 */
@Service
public class OwnerServiceImpl implements IOwnerService {
    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    private OwnerMapper ownerMapper;

    @Override
    /**
    * TODO
    *  货主登录
    * @params：
     * @param ownerName
     * @param password
    * @return {@link String}
    * @Author: laobian
    * @Date: 9:32 2021/6/23
    */
    public OwnerDTO login(String ownerName, String password){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("account",ownerName);
        map.put("password",password);
        Owner owner = ownerMapper.findOwnerByNP(map);
        if(Objects.isNull(owner)){
            return  null;
        }
        Long id = owner.getOwnerId();
        owner.setPassword(null);
        String userId = "o"+id.toString();
        String token  = jwtUtil.generateToken(userId);
        CacheUtil.put(userId,token);
        int type = owner.getIdenfity();
        OwnerDTO ownerDTO = new OwnerDTO(type,token,owner);
        return ownerDTO;
    }
}
